package com.youtui.manager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youtui.common.constant.SystemConstant;
import com.youtui.common.service.base.impl.BaseServiceImpl;
import com.youtui.common.util.NextIdUtil;
import com.youtui.manager.mapper.AccountInfoMapper;
import com.youtui.manager.model.AccountInfo;
import com.youtui.manager.model.AccountInfoExample;
import com.youtui.manager.model.AccountInfoExample.Criteria;
import com.youtui.manager.service.IAccountInfoService;

@Service
public class AccountInfoServiceImpl extends BaseServiceImpl implements IAccountInfoService {

	@Autowired
	AccountInfoMapper accountInfoMapper;

	@Override
	public AccountInfo findAccountInfoByDevId(Long devId) {

		if (devId == null) {
			return null;
		}
		AccountInfoExample example = new AccountInfoExample();
		Criteria criteria = example.createCriteria();

		criteria.andDevIdEqualTo(devId);

		List<AccountInfo> list = accountInfoMapper.selectByExample(example);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public AccountInfo add(Long devId, Long accountId) {

		AccountInfo accountInfo = new AccountInfo();
		accountInfo.setId(NextIdUtil.getNextId());
		accountInfo.setDevId(devId);
		accountInfo.setAccountId(accountId);
		accountInfo.setStatus(SystemConstant.status_effect);
		accountInfoMapper.insert(accountInfo);
		return accountInfo;
	}

	@Override
	public AccountInfo update(AccountInfo accountInfo) {

		accountInfoMapper.updateByPrimaryKeySelective(accountInfo);
		return accountInfo;
	}

}
